<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Order, Business, Config, OrderGoods, Goods, GoodsSpec, User as Users};
use think\Db;
use think\facade\Log;
use app\facade\Redis;

class OrderIncome extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('OrderIncome')->setDescription('订单结算');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        $intergal = (new Config())->where('name', 'intergal')->value('value') ?? 1;
        (new Order())->where(['status' => 4, 'incomestatus' => 1])->chunk(10, function ($lists) use ($intergal) {
            foreach ($lists as $list) {
                $frtimes = Redis::get('orderincome' . $list['id']);
                if ($frtimes) {
                    dump(1);
                    continue;
                } else {
                    Redis::setex('orderincome' . $list['id'], 5, 5);
                }
                $goodsids = [];
                $goodsspecids = [];
                $goodslist = (new OrderGoods())->where('ordersn', $list['ordersn'])->select();
                foreach ($goodslist as $goodslists) {
                    $goodsids[] = $goodslists['goodsid'];
                    $goodsspecids[] = $goodslists['goodspecid'];
                }
                switch ($list['payment']) {
                    case '0':
                        Db::startTrans();
                        try {
                            (new Goods())->where(['id' => $goodsids])->setInc('sale');
                            (new GoodsSpec())->where(['id' => $goodsspecids])->setInc('sales');
                            (new Order())->where('id', $list['id'])->update(['incomestatus' => 2]);
                            Db::commit();
                        } catch (\Exception $e) {
                            Db::rollback();
                            echo $e->getMessage();
                        }
                        echo '结算成功';
                        break;
                    case '1':
                        $intergal_num = floor($list['total'] * $intergal * 100) / 100;
                        Db::startTrans();
                        try {
                            (new Goods())->where(['id' => $goodsids])->setInc('sale');
                            (new GoodsSpec())->where(['id' => $goodsspecids])->setInc('sales');
                            (new Order())->where('id', $list['id'])->update(['incomestatus' => 2]);
                            (new Business())->MoneyEdit($list['businessid'], 1, $list['total'], '订单结算', $list['ordersn']);
                            (new Users())->MoneyEdit($list['user_id'], 1, $intergal_num, '订单赠送积分', $list['ordersn']);
                            Db::commit();
                        } catch (\Exception $e) {
                            Db::rollback();
                            echo $e->getMessage();
                        }
                        echo '结算成功';
                        break;
                    default:
                        break;
                }
            }
        });
    }
}